package v3.chapter2.doubleSynBlockOneTwo;
 
public class Run {

	public static void main(String[] args) {
		ObjectService service =new ObjectService();
		ThreadA a=new ThreadA(service);
		a.setName("A");
		a.start();

		ThreadB b=new ThreadB(service);
		b.setName("A");
		b.start();
	}

}


class ThreadB extends Thread{
	private ObjectService task;
	public ThreadB(ObjectService task) {
		super();
		this.task=task;
	}
	
	@Override 
	public void run() {
		super.run();
		task.serviceMethodB();
	}
}

class ThreadA extends Thread{
	private ObjectService task;
	public ThreadA(ObjectService task) {
		super();
		this.task=task;
	}
	
	@Override 
	public void run() {
		super.run();
		task.serviceMethodA();
	}
}


class ObjectService {
	public void serviceMethodA() {
		try {
			synchronized (this) {
				System.out.println("A begin time="+System.currentTimeMillis());
				Thread.sleep(2000);
				System.out.println("A end time="+System.currentTimeMillis());
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void serviceMethodB() {
		try {
			synchronized (this) {
				System.out.println("B begin time="+System.currentTimeMillis());
				Thread.sleep(2000);
				System.out.println("B end time="+System.currentTimeMillis());
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}